package com.unieagle.exam.model;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;

import com.unieagle.exam.utils.SharedPreferenceUtils;

public class Exam implements Parcelable{

	private long startTime;
	
	private long endTime;
	
	private int total_cnt;
	
	private int correct_cnt;
	
	private List<Question> questions;
	
	public Exam() {
		questions = new ArrayList<Question>();
		startTime = System.currentTimeMillis();
		total_cnt = 0;
		correct_cnt = 0;
	}

	public long getStartTime() {
		return startTime;
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}

	public long getEndTime() {
		return endTime;
	}

	public void setEndTime(long endTime) {
		this.endTime = endTime;
	}

	public int getTotal_cnt() {
		return total_cnt;
	}

	public void setTotal_cnt(int total_cnt) {
		this.total_cnt = total_cnt;
	}

	public int getCorrect_cnt() {
		return correct_cnt;
	}

	public void setCorrect_cnt(int correct_cnt) {
		this.correct_cnt = correct_cnt;
	}

	public boolean isPass() {
		for(Question q : questions) {
			if(q.isRequired() && !q.isCorrect()) {
				return false;
			}
		}
		return getScore() >= 80;
	}
	
	public int getScore() {
		int required_cnt = 0; //必知必会不计入总分
		for(Question q : questions) {
			if(q.isRequired()) {
				required_cnt++;
				if(!q.isCorrect()) {
					return 0;
				}
			} 
		}
		return (int)((float)correct_cnt / (total_cnt - required_cnt)) * 100;
	}
	
	public void calCorrectCnt() { //计算答对的非必知必会题目
		for(Question q : questions) {
			if(!q.isRequired() && q.isCorrect()) {
				correct_cnt++;
			}
		}
	}
	
	public void statErrorTimes(Context ctx) {
		for(Question q : questions) {
			if(!q.isCorrect()) {
				int count = SharedPreferenceUtils.getErrorCount(ctx,q.getId());
				SharedPreferenceUtils.setErrorCount(ctx, q.getId(), count + 1);
			}
		}
	}
	
	public int getAnswerCnt() {
		int cnt = 0;
		for(Question q : questions) {
			if(q.isAnswered()) {
				cnt++;
			}
		}
		return cnt;
	}
	
	public int calCorrectCnt2() { //错题重做计算正确题数，这个是包含必知必会的
		int cnt = 0;
		for(Question q : questions) {
			if(q.isCorrect()) {
				cnt++;
			}
		}
		return cnt;
	}
	
	public long getCaseId() {
		return questions.get(0).getCaseId();
	}

	public List<Question> getQuestions() {
		return questions;
	}

	public void setQuestions(List<Question> questions) {
		this.questions = questions;
	}
	
	public boolean isAllRight() {
		return this.correct_cnt == 100;
	}
	
	public static final Parcelable.Creator<Exam> CREATOR = new Creator<Exam>() {
		
		@Override  
		public Exam createFromParcel(Parcel source) {  
			Exam exam = new Exam();
			exam.setStartTime(source.readLong());
			exam.setEndTime(source.readLong());
			exam.setTotal_cnt(source.readInt());
			exam.setCorrect_cnt(source.readInt());
			source.readList(exam.getQuestions(), Question.class.getClassLoader());
			return exam;  
		}

		@Override
		public Exam[] newArray(int arg0) {
			return new Exam[arg0];
		}
	};

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel arg0, int arg1) {
		arg0.writeLong(startTime);
		arg0.writeLong(endTime);
		arg0.writeInt(total_cnt);
		arg0.writeInt(correct_cnt);
		arg0.writeList(questions);
	}
	
}
